﻿@page "/sdk/objects/entity/r/{ParentRecordId}/rl/data/l"
@using WebVella.Erp.Api.Models;
@model WebVella.Erp.Plugins.SDK.Pages.ErpEntity.EntityDataModel
@using WebVella.Erp.Web.Utils;
@{
    Layout = "~/Pages/_AppMaster.cshtml";
    ViewData["Title"] = "Entity data";
    var titleAction = "<a href=\"javascript:void(0)\" class=\"clear-filter-all\">clear all</a>";
    var geoFieldRendered = false;
}
<wv-page-header color="@Model.ErpEntity.Color" area-label="Entities" title="@Model.ErpEntity.Name" subtitle="Data"
				icon-class="@Model.ErpEntity.IconName" return-url="@Model.ReturnUrl">
	<wv-page-header-actions>

		@if (@Model.CreateAccess)
		{
			<a href='/sdk/objects/entity/r/@Model.ErpEntity.Id/rl/data/c/?returnUrl=@Model.ReturnUrlEncoded' class='btn btn-white btn-sm'><span class='fa fa-plus go-green'></span> Create Record</a>
		}
		<button type='button' onclick="ErpEvent.DISPATCH('WebVella.Erp.Web.Components.PcDrawer','open')" class='btn btn-white btn-sm'><span class='fa fa-search'></span> Search</button>
	</wv-page-header-actions>

	<wv-page-header-toolbar>
		@foreach (var action in Model.HeaderToolbar)
		{
			@Html.Raw(action)
		}
	</wv-page-header-toolbar>

</wv-page-header>

<wv-validation errors="@Model.Validation.ToErrorList()" message="@Model.Validation.Message"></wv-validation>

<wv-grid bordered="true" hover="true" columns="Model.Columns" page="Model.Pager" total-count="Model.TotalCount" page-size="Model.PagerSize" small="true">
	@foreach (var record in Model.Records)
	{
		var recordId = record["id"];
		<wv-grid-row>

			<wv-grid-column text-nowrap="true">
				<wv-form label-mode="Stacked" mode="Form" name="DeleteRecord" validation-errors="Model.Validation.ToErrorList()" id="DeleteRecord" action="/sdk/objects/entity/r/@Model.ErpEntity.Id/rl/data/l/?recordId=@recordId" method="Post">
					@if (@Model.UpdateAccess)
					{
						<wv-button type="LinkAsButton" href="/sdk/objects/entity/r/@Model.ErpEntity.Id/rl/data/m/@record["id"]?returnUrl=@Model.ReturnUrlEncoded" icon-class="icon fa fa-pencil-alt fa-fw"></wv-button>
					}
					@if (@Model.DeleteAccess)
					{
						<wv-button type="Submit" icon-class="icon fa fa-trash fa-fw" onclick="return confirm('Are you sure ?')"></wv-button>
					}
				</wv-form>
			</wv-grid-column>

			@foreach (var field in Model.Fields)
			{
				var fieldType = field.GetFieldType();
				var value = record[field.Name];

				var wrap = fieldType == FieldType.TextField || fieldType == FieldType.HtmlField || fieldType == FieldType.MultiSelectField ||
							fieldType == FieldType.MultiLineTextField || fieldType == FieldType.UrlField;

        <wv-grid-column text-nowrap="@(!wrap)">
            @switch (fieldType)
            {
                case FieldType.AutoNumberField:
                    {
                        <wv-field-autonumber value="@value"
                                             mode="Simple"
                                             name="@field.Name"
                                             label-text="@field.Label"
                                             label-help-text="@field.HelpText"
                                             description="@field.Description"
                                             placeholder="@field.PlaceholderText"
                                             required="@field.Required"
                                             access="@(Model.GetFieldAccess(field))"
                                             template="@(((AutoNumberField)field).DisplayFormat)"></wv-field-autonumber>
                    }
                    break;

                case FieldType.TextField:
                    {
                        <wv-field-text label-text="@field.Label"
                                       name="@field.Name"
                                       value="@value"
                                       mode="Simple"
                                       access="@(Model.GetFieldAccess(field))"
                                       label-help-text="@field.HelpText"
                                       description="@field.Description"
                                       placeholder="@field.PlaceholderText"
                                       required="@field.Required"
                                       maxlength="@(((TextField)field).MaxLength)"></wv-field-text>
                    }
                    break;

                case FieldType.MultiLineTextField:
                    {
                        <wv-field-textarea label-text="@field.Label"
                                           name="@field.Name"
                                           value="@value"
                                           mode="Simple"
                                           access="@(Model.GetFieldAccess(field))"
                                           label-help-text="@field.HelpText"
                                           description="@field.Description"
                                           placeholder="@field.PlaceholderText"
                                           required="@field.Required"></wv-field-textarea>
                    }
                    break;
                case FieldType.GeographyField:
                    {
                        var id = record["id"];
                        var geojson = "";
                        var text = "";
                        GeographyField geo = (GeographyField)field;
                        if (geo.Format == GeographyFieldFormat.GeoJSON && record[field.Name] != null)
                        {
                            geojson = record[field.Name].ToString();
                        }
                        else if (geo.Format == GeographyFieldFormat.Text && record[field.Name] != null)
                        {
                            text = record[field.Name].ToString();
                        }
                        var mapid = $"map_{record["id"].ToString().Replace("-", "")}";

                        if (!geoFieldRendered)
                        {
                            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.6.0/leaflet.css" />
                            <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
                            <script>
                                function showMap(mid) {
                                    var m = $('#' + mid);
                                    var geojson = m.data('geojson');
                                    var text = m.data('text');

                                    if (geojson || text) {

                                        var map = L.map(mid);
                                        L.tileLayer('http://a.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                                            attribution: '&copy; <a href="http://openstreetmap.org">OSM</a>',
                                            maxZoom: 18,
                                        }).addTo(map);
                                        var geojson = m.data('geojson');
                                        var text = m.data('text');

                                        var layer = L.geoJson(geojson).addTo(map);
                                        map.fitBounds(layer, { padding: [-5, -5] });
                                    }
                                }
                            </script>
                        }
                        if (!geoFieldRendered)
                        {
                            geoFieldRendered = true;
                        }
                        if (!string.IsNullOrEmpty(geojson) || !string.IsNullOrEmpty(text))
                        {
                            <div id="@mapid" style="height:200px;width:200px" data-geojson="@geojson" data-text="@text"></div>
                            <script>
                            showMap('@mapid');
                            </script>
                        }
                    }
                    break;

                case FieldType.GuidField:
                    {
                        <wv-field-guid label-text="@field.Label"
                                       name="@field.Name"
                                       value="@value"
                                       mode="Simple"
                                       access="@(Model.GetFieldAccess(field))"
                                       label-help-text="@field.HelpText"
                                       description="@field.Description"
                                       placeholder="@field.PlaceholderText"
                                       required="@field.Required"></wv-field-guid>
                    }
                    break;

                case FieldType.EmailField:
                    {
                        <wv-field-email label-text="@field.Label"
                                        name="@field.Name"
                                        value="@value"
                                        mode="Simple"
                                        access="@(Model.GetFieldAccess(field))"
                                        label-help-text="@field.HelpText"
                                        description="@field.Description"
                                        placeholder="@field.PlaceholderText"
                                        required="@field.Required"
                                        maxlength="@(((EmailField)field).MaxLength)"></wv-field-email>
                    }
                    break;

                case FieldType.UrlField:
                    {
                        <wv-field-url label-text="@field.Label"
                                      name="@field.Name"
                                      value="@value"
                                      mode="Simple"
                                      access="@(Model.GetFieldAccess(field))"
                                      label-help-text="@field.HelpText"
                                      description="@field.Description"
                                      placeholder="@field.PlaceholderText"
                                      required="@field.Required"></wv-field-url>
                    }
                    break;

                case FieldType.NumberField:
                    {
                        <wv-field-number label-text="@field.Label"
                                         name="@field.Name"
                                         value="@value"
                                         mode="Simple"
                                         access="@(Model.GetFieldAccess(field))"
                                         label-help-text="@field.HelpText"
                                         description="@field.Description"
                                         placeholder="@field.PlaceholderText"
                                         required="@field.Required"
                                         min="@(((NumberField)field).MinValue)"
                                         max="@(((NumberField)field).MaxValue)"
                                         decimal-digits="@((int?)((NumberField)field).DecimalPlaces ?? 0)"></wv-field-number>
                    }
                    break;

                case FieldType.CheckboxField:
                    {
                        <wv-field-checkbox name="@field.Name"
                                           value="@value"
                                           mode="Simple"
                                           access="@(Model.GetFieldAccess(field))"
                                           label-help-text="@field.HelpText"
                                           description="@field.Description"
                                           placeholder="@field.PlaceholderText"
                                           required="@field.Required"></wv-field-checkbox>
                    }
                    break;

                case FieldType.CurrencyField:
                    {
                        <wv-field-currency label-text="@field.Label"
                                           name="@field.Name"
                                           value="@value"
                                           mode="Simple"
                                           access="@(Model.GetFieldAccess(field))"
                                           label-help-text="@field.HelpText"
                                           description="@field.Description"
                                           placeholder="@field.PlaceholderText"
                                           required="@field.Required"
                                           currency-code="@(((CurrencyField)field).Currency.Code)"
                                           min="@(((CurrencyField)field).MinValue)"
                                           max="@(((CurrencyField)field).MaxValue)"></wv-field-currency>
                    }
                    break;

                case FieldType.DateField:
                    {
                        <wv-field-date label-text="@field.Label"
                                       name="@field.Name"
                                       value="@value"
                                       mode="Simple"
                                       access="@(Model.GetFieldAccess(field))"
                                       label-help-text="@field.HelpText"
                                       description="@field.Description"
                                       placeholder="@field.PlaceholderText"
                                       required="@field.Required"></wv-field-date>
                    }
                    break;

                case FieldType.DateTimeField:
                    {
                        <wv-field-datetime label-text="@field.Label"
                                           name="@field.Name"
                                           value="@value"
                                           mode="Simple"
                                           access="@(Model.GetFieldAccess(field))"
                                           label-help-text="@field.HelpText"
                                           description="@field.Description"
                                           placeholder="@field.PlaceholderText"
                                           required="@field.Required"></wv-field-datetime>
                    }
                    break;

                case FieldType.FileField:
                    {
                        <wv-field-file label-text="@field.Label"
                                       name="@field.Name"
                                       value="@value"
                                       mode="Simple"
                                       access="@(Model.GetFieldAccess(field))"
                                       label-help-text="@field.HelpText"
                                       description="@field.Description"
                                       placeholder="@field.PlaceholderText"
                                       required="@field.Required"></wv-field-file>
                    }
                    break;

                case FieldType.ImageField:
                    {
                        <wv-field-image label-text="@field.Label"
                                        name="@field.Name"
                                        value="@value"
                                        mode="Simple"
                                        access="@(Model.GetFieldAccess(field))"
                                        label-help-text="@field.HelpText"
                                        description="@field.Description"
                                        placeholder="@field.PlaceholderText"
                                        required="@field.Required"></wv-field-image>
                    }
                    break;

                case FieldType.HtmlField:
                    {
                        <wv-field-html label-text="@field.Label"
                                       name="@field.Name"
                                       value="@value"
                                       mode="Simple"
                                       access="@(Model.GetFieldAccess(field))"
                                       label-help-text="@field.HelpText"
                                       description="@field.Description"
                                       placeholder="@field.PlaceholderText"
                                       required="@field.Required"></wv-field-html>
                    }
                    break;

                case FieldType.PasswordField:
                    {
                        <wv-field-password label-text="@field.Label"
                                           name="@field.Name"
                                           value="@value"
                                           mode="Simple"
                                           access="@(Model.GetFieldAccess(field))"
                                           label-help-text="@field.HelpText"
                                           description="@field.Description"
                                           placeholder="@field.PlaceholderText"
                                           required="@field.Required"
                                           min="@(((PasswordField)field).MinLength)"
                                           max="@(((PasswordField)field).MaxLength)"></wv-field-password>
                    }
                    break;

                case FieldType.PercentField:
                    {
                        <wv-field-percent label-text="@field.Label"
                                          name="@field.Name"
                                          value="@value"
                                          mode="Simple"
                                          access="@(Model.GetFieldAccess(field))"
                                          label-help-text="@field.HelpText"
                                          description="@field.Description"
                                          placeholder="@field.PlaceholderText"
                                          required="@field.Required"
                                          min="@(((PercentField)field).MinValue)"
                                          max="@(((PercentField)field).MaxValue)"
                                          decimal-digits="@((int?)((PercentField)field).DecimalPlaces ?? 0)"></wv-field-percent>
                    }
                    break;

                case FieldType.PhoneField:
                    {
                        <wv-field-phone label-text="@field.Label"
                                        name="@field.Name"
                                        value="@value"
                                        mode="Simple"
                                        access="@(Model.GetFieldAccess(field))"
                                        label-help-text="@field.HelpText"
                                        description="@field.Description"
                                        placeholder="@field.PlaceholderText"
                                        required="@field.Required"
                                        maxlength="@(((PhoneField)field).MaxLength)"></wv-field-phone>
                    }
                    break;

                case FieldType.SelectField:
                    {
                        <wv-field-select label-text="@field.Label"
                                         name="@field.Name"
                                         value="@value"
                                         mode="Simple"
                                         access="@(Model.GetFieldAccess(field))"
                                         label-help-text="@field.HelpText"
                                         description="@field.Description"
                                         placeholder="@field.PlaceholderText"
                                         required="@field.Required"
                                         default-value="@(((SelectField)field).DefaultValue)"
                                         options="@(((SelectField)field).Options).ToWvSelectOption()"></wv-field-select>
                    }
                    break;

                case FieldType.MultiSelectField:
                    {
                        <wv-field-multiselect label-text="@field.Label"
                                              name="@field.Name"
                                              value="@value"
                                              mode="Simple"
                                              access="@(Model.GetFieldAccess(field))"
                                              label-help-text="@field.HelpText"
                                              description="@field.Description"
                                              placeholder="@field.PlaceholderText"
                                              required="@field.Required"
                                              default-value="@(((MultiSelectField)field).DefaultValue)"
                                              options="@(((MultiSelectField)field).Options).ToWvSelectOption()"></wv-field-multiselect>
                    }
                    break;

            }
        </wv-grid-column>
			}
		</wv-grid-row>
	}
	@if (Model.Records.Count == 0)
	{
		<tbody>
			<tr>
				<td colspan="@Model.Columns.Count"><div class="alert alert-info">No records found</div></td>
			</tr>
		</tbody>
	}
</wv-grid>

<wv-drawer width="550px" title="Search Fields" title-action-html="@titleAction">
	<form method="get" name="SearchEntities">
		@foreach (var filter in Model.Filters)
		{
			<wv-filter-text name="@filter.Name" label="@filter.Label" query-type="@filter.Type" query-options="@filter.AllowedTypes"></wv-filter-text>
		}
		<hr class="divider" />

		<button type="submit" class="btn btn-sm btn-white">Search Fields</button>
	</form>
</wv-drawer>